#pragma once
#include<pthread.h>
#include<iostream>

//封装一下锁
class Mutex
{
public:
    Mutex(pthread_mutex_t* mutex = nullptr)
    :_mutex(mutex)
    {       
    }

    void Lock()
    {
        if(_mutex)
        pthread_mutex_lock(_mutex);
    }
    void UnLock()
    {
        if(_mutex)
        pthread_mutex_unlock(_mutex);
    }

private:
    pthread_mutex_t* _mutex;
};

class LockGuard
{
public:
    LockGuard(pthread_mutex_t* mutex)
    :_mutex(mutex)
    {
        //构造中加锁
        _mutex.Lock();
    }
    ~LockGuard()
    {
        //析构中解锁
        _mutex.UnLock();
    }
private:
    Mutex _mutex;
};


